# Makefile - Top-level makefile.
# Copyright (C) 2008  Mikhail Vorozhtsov
# See the LICENSE section of the README file for details.

# $Id$

ifndef SBE_BASE
  $(error SBE_BASE is not defined)
endif

include $(SBE_BASE)/mk/sbe.mk

$(call sbe-init,0,0)
$(call sbe-self,libbst,0,0)

$(call sbe-dep,libcompiler,0,0)

SEDFLAGS := \
  -e "s/$($(SELF)_NAME_UC)_PREFIX/bst-$($(SELF)_VER_MAJOR)/" \
  -e "s/$($(SELF)_NAME_UC)_NS/bst_$($(SELF)_VER_MAJOR)/" \
  -e "s/$($(SELF)_NAME_UC)_MJ/$(SELF)/"

IN_HDRS := $(shell find headers -type f -name '*.hh')
OUT_HDRS := \
  $(call subst-dirs,headers dep-headers \
                    bst bst-$($(SELF)_VER_MAJOR), \
                    $(IN_HDRS))

override TEST_CPPFLAGS := \
  $(CPPFLAGS) \
  $(SBE_CPPFLAGS) \
  -I dep-headers \
  -DLIBBST_PREFIX=bst-$($(SELF)_VER_MAJOR) \
  -DLIBBST_NS=bst_$($(SELF)_VER_MAJOR) \
  -DLIBBST_MJ=$(SELF)
override TEST_CXXCPPFLAGS := \
  $(CXXCPPFLAGS) $(call sbe-dep-var,libcompiler,CXXCPPFLAGS)
override TEST_CXXFLAGS := \
  $(TEST_CXXFLAGS) \
  -std=gnu++98 \
  -Wall -Wextra -Wno-pointer-arith -Wno-invalid-offsetof -Werror

TEST_EXE := test.all

TEST_SRCS := sources/test/test-all.cc
TEST_OBJS := $(call obj-files,$(TEST_SRCS))
TEST_DEPS := $(TEST_OBJS:%.o=%.d)

default: all

ifneq ($(MAKECMDGOALS),clean)
-include $(TEST_DEPS)
endif

$(call sed-rules,$(IN_HDRS),$(OUT_HDRS))
$(call cxx-rules,$(TEST_SRCS),$(TEST_OBJS),TEST)

$(TEST_EXE): $(TEST_OBJS)
	$(CXX) -o $@ $(TEST_OBJS)

.PHONY: test
test: all $(TEST_EXE)
	./$(TEST_EXE)

.PHONY: all
all: $(OUT_HDRS)

.PHONY: clean
clean:
	rm -rf dep-headers $(TEST_EXE) $(TEST_OBJS) $(TEST_DEPS)

